/**
 * 
 */
package com.fwzc.giuseppe.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.base.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * 
 * 类名称：FindPwActivity 类描述： 创建人：何琳 创建时间：2015年8月13日 下午12:25:20
 * 
 * @version
 * 
 */
public class FindPwActivity extends BaseActivity implements OnClickListener {
	private TextView findpw;
	private EditText username, code, password;
	private HttpUtils httpUtil;
	private ProgressDialog load;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_findpw);

		initBar();
		initView();
		initData();
	}

	private void initView() {
		httpUtil = new HttpUtils();
		load = new ProgressDialog(this);
		load.setMessage(getResources().getString(R.string.deal));
		load.setCancelable(false);
		
		findpw = (TextView) findViewById(R.id.findpw_btn);
		username = (EditText) findViewById(R.id.fpw_username);
		code = (EditText) findViewById(R.id.fpw_code);
		password = (EditText) findViewById(R.id.fpw_password);
	}

	private void initData() {
		actionbar_side_name.setText("忘记密码");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		findpw.setOnClickListener(this);
		iv_actionbar_side_left.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == findpw) {
			sendRequest();
		} else if (v == iv_actionbar_side_left) {
			finish();
		}

	}

	// 忘记密码
	private void sendRequest() {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("userName", username.getText().toString());
		params.addBodyParameter("authenticationCode", code.getText().toString());
		params.addBodyParameter("newPassword", password.getText().toString());
		// 发送json类型的数据
		// params.setBodyEntity(new StringEntity(jsonStr,charset));
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.FORGET_PASSWORD, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 0) {
								load.dismiss();
								showToast("密码修改成功");
							} else {
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}

					}
				});
	}

	public void getCode(View v) {
		if ("".equals(username.getText().toString())) {
			showToast("请填写手机号");
			return;
		}

		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("phoneNumber", username.getText().toString());
		// 发送json类型的数据
		// params.setBodyEntity(new StringEntity(jsonStr,charset));
		httpUtil.send(HttpRequest.HttpMethod.POST, Api.GET_CODE, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {

					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						showToast("加载数据失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(
									responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 0) {
								showToast("获取验证码成功");
							}
						} catch (JSONException e) {
							showToast("解析出错");
						}

					}
				});
	}
}
